
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 用户管理
 * @author lxn
 */
class User extends Base_Controller
{
	private static $data = array();
	protected $models=['user_model','type_model', 'company_model'];
	public function __construct()
	{
		parent::__construct();		
	}

    /**
     *@title 微信小程序登录接口
     *@desc 微信小程序登录接口
     *@input {"name":"code","require":"true","type":"int","desc":"这个code是用wx.login接口获取到的code"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     *
     *@output {"name":"data.userId","type":"int","desc":"登录用户的ID,小程序端自己维护用户登录态,若有其他要求,再商量"}
     * */
    public function weixin_login()
    {
        $code=$this->input->post('code');
        if (!$code){
            show300('参数错误');
        }
        
        $appid = XCXID;     //小程序ID
        $appsecret = APPSECRET;     //小程序密钥
        $wx_url='https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$appsecret.'&js_code='.$code.'&grant_type=authorization_code';
        $res=$this->curl_request($wx_url);
        
        $data=json_decode($res);
        $openid=@$data->openid;
        if(!$openid){
            show400('小程序appid可能不一致,请检查后重试');
        }
        $userId=$this->getId($openid);
        if ($userId){            
            $resopne=['userId'=>$userId];
            show200($resopne);
        }else{
            show400('获取用户数据失败');
        }
    }
    
    
    
    /**
     *@title 获取用户信息
     *@desc 获取用户信息(该接口在帮客第一版中用不到,先做保留)
     *@input {"name":"userId","require":"true","type":"int","desc":"登录用户ID"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     *
     *@output {"name":"data.userId","type":"int","desc":"登录用户的ID,小程序端自己维护用户登录态,若有其他要求,再商量"}
     *@output {"name":"data.realName","type":"string","desc":"真实姓名"}
     *@output {"name":"data.sex","type":"string","desc":"用户的性别，值为1时是男性，值为2时是女性，值为0时是未知"}
     *@output {"name":"data.birthday","type":"string","desc":"生日,用户年龄那一栏建议改成用户生日,因为年龄会一直变"}
     *@output {"name":"data.workCityId","type":"string","desc":"工作城市ID"}
     *@output {"name":"data.cityName","type":"string","desc":"工作城市名称"}
     *@output {"name":"data.jobId","type":"string","desc":"期望职业分类ID"}
     *@output {"name":"data.jobName","type":"string","desc":"期望职业分类名称"}
     *@output {"name":"data.salaryId","type":"string","desc":"期望薪资分类ID"}
     *@output {"name":"data.salaryName","type":"string","desc":"期望薪资分类名称"}
     *@output {"name":"data.workStatus","type":"string","desc":"当前工作状态:1找工作 2不找工作"}
     *@output {"name":"data.salaryName","type":"string","desc":"当前工作状态分类名称"}
     *@output {"name":"data.exp","type":"string","desc":"工作经验"}
     *@output {"name":"data.intro","type":"string","desc":"个人介绍"}
     *@output {"name":"data.skill","type":"array","desc":"个人职业技能数据,数组形式"}
     *@output {"name":"data.skill[index].typeId","type":"string","desc":"技能分类ID","child":"1"}
     *@output {"name":"data.skill[index].typeName","type":"string","desc":"技能分类名称","child":"1"}
     * */
    public function getUserinfo(){
        $userId=$this->input->post('userId');
        if(!$userId){
            show3();
        }
        
        $userinfo=$this->user_model->getWhereRow(['userId'=>$userId]);
        if(!$userinfo){
            show400();
        }
        if($userinfo['skill']){
            $skills=explode('|', $userinfo['skill']);
            $skillsdata=$this->type_model->getList($skills);
            $userinfo['skill']=$skillsdata;
        }else{
            $userinfo['skill']=[];
        }
        show200($userinfo);
    }
    /**
     *@title 完善个人信息
     *@desc 完善个人信息(小程序中需要定期对用户的微信昵称和头像进行更新,以保证数据库和微信数据一致)
     *@input {"name":"userId","require":"true","type":"int","desc":"登录用户ID"}
     *@input {"name":"nickName","require":"true","type":"string","desc":"微信昵称"}
     *@input {"name":"avatar","require":"true","type":"string","desc":"微信头像链接"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     * */
    public function updateUserinfo(){
        $userId=$this->input->post('userId');        
        $nickName=$this->input->post('nickName');
        $avatar=$this->input->post('avatar');
        if(!$nickName||!$avatar){
            show401('必填参数不能为空');
        }
        if(!$userId){
            show3();
        }
        $data=[
            'nickName'=>$nickName,
            'avatar'=>$avatar
        ];
        $res=$this->user_model->updateWhere(['userId'=>$userId],$data);
        if ($res){
            show200('修改成功');
        }
        else{
            show400('修改失败');
        }                
    }
    
    
    /**
     *@title 获取用户手机号
     *@desc 获取用户手机号
     *@input {"name":"userId","require":"true","type":"int","desc":"登录用户ID"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300:用户未设置手机号"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     *
     *@output {"name":"data.userId","type":"int","desc":"用户ID"}
     *@output {"name":"data.phone","type":"string","desc":"用户手机号"}
     * */
    public function getPhone(){
        $userId=$this->input->post('userId');
        if(!$userId){
            show3();
        }
        $select='userId,phone';
        $userinfo=$this->user_model->getWhereRow(['userId'=>$userId],$select);
        if(!$userinfo){
            show400('没有该用户信息');
        }
        if(!$userinfo['phone']) show300('还未设置手机号');
        $userinfo['phone']=substr_replace($userinfo['phone'], '****', 3,4);
        show200($userinfo);
    }
    
    /**
     *@title 绑定手机号
     *@desc 绑定手机号
     *@input {"name":"userId","require":"true","type":"int","desc":"登录用户ID"}
     *@input {"name":"phone","require":"true","type":"string","desc":"用户手机号"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     * */
    public function bindPhone(){
        $userId=$this->input->post('userId');
        $phone=$this->input->post('phone');
        
        if(!$userId){
            show3();
        }
        if(!$phone){
            show300('手机号不能为空');
        }
        $res=$this->user_model->updateWhere(['userId'=>$userId],['phone'=>$phone]);
        if ($res){
            show200('修改成功');
        }
        else{
            show400('修改失败');
        }
    }
    
    /**
     *@title 检测手机号输入是否正确
     *@desc 检测手机号输入是否正确(用于用户更改手机号时输入旧手机号是否正确的判断)
     *@input {"name":"userId","require":"true","type":"int","desc":"登录用户ID"}
     *@input {"name":"phone","require":"true","type":"string","desc":"用户手机号"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     * */
    public function checkPhone(){
        $userId=$this->input->post('userId');
        $phone=$this->input->post('phone');
        
        if(!$userId){
            show3();
        }
        if(!$phone){
            show300('手机号不能为空');
        }
        $select='userId,phone';
        $userinfo=$this->user_model->getWhereRow(['userId'=>$userId],$select);
        if(@$userinfo['phone']==$phone){
            show200('手机号输入正确');
        }else{
            show400('手机号输入不正确');
        }
    }
    /**
     *@title 发送验证码
     *@desc 发送手机验证码
     *@input {"name":"phone","require":"true","type":"string","desc":"用户手机号"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     *
     *@output {"name":"data.sessionId","type":"int","desc":"sessionid,因为小程序在请求头中并没有cookie,所以要想在小程序中依然用session的话必须请求头中加入cookie参数,如'Cookie':'ci_session=4vd6svd57d5e25pfjg3ntp3k798d00rk"}
     * */
    public function sendSms(){
        $phone=$this->input->post('phone');
        if(!$phone){
            show300('手机号不能为空');
        }
        // 需要发送短信的手机号码
        $phoneNumbers = [$phone];
        
        
        // 短信模板ID，需要在短信应用中申请
        $templateId= 'SMS_141945019';  // NOTE: 这里的模板ID`7839`只是一个示例，真实的模板ID需要在短信控制台中申请
        
        // 签名
        $smsSign = "众合致胜"; // NOTE: 这里的签名只是示例，请使用真实的已申请的签名，签名参数使用的是`签名内容`，而不是`签名ID`
        $yzm=rand(1000,9999);
        $params=[$yzm];     //模板里的参数,此处即手机验证码
        //$config=['smsSign'=>$smsSign,'templateId'=>$templateId,'phoneNumbers'=>$phoneNumbers,'params'=>$params];
        //$this->load->library('sms/api_demo/SmsDemo');
        require '/application/libraries/sms/api_demo/SmsDemo.php';
        $sms=new SmsDemo();
        $res=$sms->sendSms($phone,$templateId,$smsSign,['code'=>$yzm]);
        if($res->Code=='OK'){
            $this->session->set_tempdata('yzm', $yzm,60);
            $sessionId=session_id();          //sessionid,因为小程序在请求头中并没有cookie,所以要想在小程序中依然用session的话必须请求头中加入cookie参数,如'Cookie': 'ci_session=4vd6svd57d5e25pfjg3ntp3k798d00rk'
            show200(['sessionId'=>$sessionId],'发送成功');
        }else{
            //var_dump($res);
            show400('发送失败');
            
        }        
    }
    
    /**
     *@title 检测验证码是否正确
     *@desc 检测验证码是否正确
     *@input {"name":"phone","require":"true","type":"string","desc":"用户手机号"}
     *@input {"name":"yzm","require":"true","type":"string","desc":"输入的手机验证码"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"信息说明"}
     * */
    public function checkSms(){
        $phone=$this->input->post('phone');
        $yzm=$this->input->post('yzm');
        if(!$phone){
            //show300('手机号不能为空');
        }
        if(!$yzm){
            show300('验证码不能为空');
        }
        if($yzm==$this->session->tempdata('yzm')){
            show200([],'验证码输入正确');
        }else{
            show400('验证码输入错误');
        }
    }
    
    
    
    //如果未注册则自动注册并返回用户ID,如果已注册则直接返回用户ID
    private function getId($openid){
        $user=$this->user_model->is_reg($openid);
        if($user&&$user['userId']){ //已注册
            return $user['userId'];
        }else{
            $userId=$this->user_model->insert(['wxId'=>$openid,'userFlag'=>3]);     //3为帮客普通用户标记
            return $userId;
        }
    }
    
    //curl
    private function curl_request($url,$timeout=30,$header=array()){
        if (!function_exists('curl_init')) {
            throw new Exception('server not install curl');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        $data = curl_exec($ch);
        @curl_close($ch);
        return $data;exit();
        
    }

    //获取下过单的店铺
    // post ：userId ：用户ID  limit：每页条数  page:页数
    public function getMyCompany()
    {
        $userId=$this->input->post('userId');
        if (!$userId){
            show3();
        }
        $company = $this->company_model->getMyCompany($userId);
        show200($company);
    }
	
	


}
/* End of file Login.php */